﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using System.IO;
using System.Data.Linq;



namespace QuanLyKhachSan
{
    public partial class frmKhachHang : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext dc = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString );
        KHACH_HANG kh;
        public frmDangKyThuePhong frmDangKyThuePhong;
        public bool kiemtrathemnhanhThuePhong = false;
        public frmKhachHang()
        {
            InitializeComponent();
        }

        private byte[] ImageToByteArray(Image imageIn)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();
            }
        }

        public Image ByteArrayToImage(byte[] byteArrayIn)
        {
            using (MemoryStream ms = new MemoryStream(byteArrayIn))
            {
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
        }

        public void displayInfo(KHACH_HANG kh)
        {
            txtCMND.Text = kh.KH_CMND;
            txtDiaChi.Text = kh.KH_DIACHI;
            txtDienThoai.Text = kh.KH_DIENTHOAI;
            txtHoTen.Text = kh.KH_HOTEN;
            if (kh.KH_HINH != null)
            {
                picHinhAnh.Image = ByteArrayToImage(kh.KH_HINH.ToArray());
            }
            else
            {
                picHinhAnh.Image = null;
            }

            btThem.Enabled = false;
            btLuu.Enabled = true;
            btXoa.Enabled = true;
        }

        private void gridViewKhachHang_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            kh = (KHACH_HANG)gvKhachHang.GetFocusedRow();
            displayInfo(kh);
        }

        private void gvKhachHang_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            kh = (KHACH_HANG)gvKhachHang.GetRow(e.RowHandle);
            displayInfo(kh);
        }

        private void gridViewKhachHang_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "")
            {
                if (e.RowHandle >= 0)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }

        private void frmLoad()
        {
            var khachhang = from k in dc.KHACH_HANGs  orderby k.KH_ID descending , k.KH_HOTEN ascending  select k;
            gcKhachHang.DataSource = khachhang;

            btThem.Enabled = true;
            btLuu.Enabled = false;
            btXoa.Enabled = false;

            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtCMND.Text = "";
            txtDienThoai.Text = "";
            picHinhAnh.Image = null;
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            frmLoad();
        }       

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                kh = new KHACH_HANG();
                kh.KH_HOTEN = txtHoTen.Text;
                kh.KH_DIACHI = txtDiaChi.Text;
                kh.KH_DIENTHOAI = txtDienThoai.Text;
                kh.KH_CMND = txtCMND.Text;
                if (picHinhAnh.Image != null)
                {
                    kh.KH_HINH = ImageToByteArray(picHinhAnh.Image);
                }
                else
                {
                    kh.KH_HINH = null;
                }
                dc.KHACH_HANGs.InsertOnSubmit(kh);
                dc.SubmitChanges();

                frmLoad();

            }
            catch(Exception ex)
            {
                if (ex.Message.Contains("Violation of UNIQUE KEY constraint 'IX_KHACH_HANG'"))
                {
                    XtraMessageBox.Show("Số chứng minh nhân dân này đã tồn tại.", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    throw;
                }
            }
        }
        
        private void btLuu_Click(object sender, EventArgs e)
        {
            try
            {
                kh.KH_HOTEN = txtHoTen.Text;
                kh.KH_DIACHI = txtDiaChi.Text;
                kh.KH_DIENTHOAI = txtDienThoai.Text;
                kh.KH_CMND = txtCMND.Text;
                if (picHinhAnh.Image != null)
                {
                    try
                    {
                        kh.KH_HINH = ImageToByteArray(picHinhAnh.Image);
                    }
                    catch (Exception ex)
                    {
                        if (!ex.Message.Contains("A generic error occurred in GDI+"))
                        {
                            throw;
                        }
                    }
                }
                else
                {
                    kh.KH_HINH = null;
                }
                
               
                dc.SubmitChanges();

                frmLoad();
            }
            catch (Exception)
            {
                throw;
            }
            
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("Xóa khách hàng " + kh.KH_HOTEN.ToUpper() + " ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (kh.DAT_PHONGs.Count() > 0 || kh.THUE_PHONGs.Count() > 0 || kh.TRU_TAIs.Count() > 0)
                {
                    XtraMessageBox.Show("Không thể xóa! Dữ liệu đã được sử dụng.", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    try
                    {
                        dc.KHACH_HANGs.DeleteOnSubmit(kh);
                        dc.SubmitChanges();
                        gvKhachHang.DeleteSelectedRows();
                        frmLoad();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            frmLoad();
        }

        private void frmKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (kiemtrathemnhanhThuePhong == true )
            {
                frmDangKyThuePhong.fillDataRepLookup();
            }
          
        }

                       
    }
}